<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: AdminController.php 444 2011-12-29 03:27:55Z beimuaihui@gmail.com $
 */



class Mall_AdminController extends Baogg_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		

		$Permission=new Permission();
		$User=new User();	
		$Menu=new Menu();
		$Role=new Role(); 
		//find user's group ids  
		$UserGroup=new UserGroup();
		$UserGroupRole=new UserGroupRole();	
		if($User->isAdmin()){
			$rs_group=$UserGroup->getList(array("state"=>1));			
		}else{					
			$rs_group=$UserGroupRole->getList(array("user_id"=>$_SESSION['admin']['user']['user_id']));
		}
		$arr_group=array();
		foreach((array)$rs_group as $v){
			$arr_group[]=$v['user_group_id'];
		}
		if(!$arr_group){
			throw new Exception(BAOGG_LANGUAGE::get('deny_access'));
			exit;
		}
		
		//generate user group's  Tree Combo
		$combo_group=$UserGroup->getTreeByIds($arr_group);
		if(in_array(0,$arr_group)){
			$combo_group=array_merge(array(array(0,BAOGG_LANGUAGE::get('personal'))),$combo_group);
		}
		$this->view->store_group=Zend_Json::encode($combo_group);
		
		
		//initialize user group
		//var_dump($this->getRequest()->getParam('group'));exit;
		$init_group=(int)$this->getRequest()->getParam('group');
		if($init_group===NULL || !in_array($init_group,$arr_group)){
			$init_group=NULL;			
			$level_count=0;					
			foreach($combo_group as $v){
				if($init_group===NULL){
					$init_group=$v[0];		
				}		
				//string "--" stands for level 			
				if(substr_count($v[1],'-')>$level_count){					
					$init_group=$v[0];
					$level_count=substr_count($v[1],'-');
					//var_dump(substr_count($v[1],'-'));var_dump($v[0]);var_dump($this->view->init_group);	
				}
			}	
		}		
		//var_dump($this->view->init_group);			exit;
		$_SESSION['admin']['user_group_role']['user_group_id']=$UserGroup->getWithChildIds( array($init_group));
		
		//get user's role ids
		$_SESSION['admin']['user_group_role']['role_id']=array();
		//echo '<pre>';print_r($_SESSION);exit;
		if(!$User->isAdmin()){		
			//if personal,then all parent group is himself
			if($init_group==0){
				$user_group_pids=0;
			}else{	
				$rs_user_group_pids=$UserGroup->getOne((int)$init_group);
				$user_group_pids=	array_filter(explode(",",$rs_user_group_pids[0]['user_group_pids']));
			}		
			$rs_role=$UserGroupRole->getList(array("user_id"=>$_SESSION['admin']['user']['user_id'],'user_group_id'=>$user_group_pids));
		
		}else{
			$rs_role=$Role->getList(array("state"=>1));
		}
		foreach((array)$rs_role as $v){
			$_SESSION['admin']['user_group_role']['role_id'][]=$v['role_id'];
		}
		//echo Baogg_Db::debugDb(Zend_Registry::get('db'));
		
		
		//generate bakcend menu
		 $top_where=array("key_name"=>"backend","state"=>1);
		 $rs_top_menu=$Menu->getList($top_where);
		 
		 $where=array("menu_pids like '%,{$rs_top_menu[0]['menu_id']},%'","state"=>1,"is_show"=>1);		 
		 if(!$User->isAdmin()){
		 	$arr_menu_id=$Permission->checkResource('menu');		 	
		 	if(!$arr_menu_id){
		 		$where=array("1=0");
		 	}else{		 		
		 		$rs_menu_pid=$Menu->getList(array("menu_id"=>array_keys($arr_menu_id)));		 		
		 		//all menu ids
		 		$str_menu_pids="";
		 		foreach((array)$rs_menu_pid as $v){
		 			$str_menu_pids.=$v['menu_pids'];
		 		}		 		
		 		$where['menu_id']=array_filter(array_unique(explode(",",$str_menu_pids)));		 		
		 	}
		 }        
       
        $rs=$Menu->getList($where,array("sort"=>"ordering","dir"=>"asc"),null,array("menu_id","menu_name","menu_pid","*"));
       // echo '<pre>';print_r($where);print_r($rs);exit;
        
        $objTree=new Baogg_Db_Tree();
        $tr=$objTree->rs2GridTree($rs);
        $rs=$objTree->tr2GridStore($tr);
        //echo '<pre>';print_r($rs);exit;
        $rs_menu=array();
        $pid=0;
        $mall_menu_id=0;
        $edu_menu_id=0;
        foreach((array)$rs as $k=>$v){
        	if($v['key_name']=='backend'){
        		continue;
        	}
        	if($v['key_name']=='mall'){
        		$mall_menu_id=$v['menu_id'];
        	}else if($v['key_name']=='edu'){
        		$edu_menu_id=$v['menu_id'];
        	}
        	$pid=$pid?$pid:'menu_'.$v['menu_pid'];
        	$rs_menu[]=array(
        		"id"=>'menu_'.$v['menu_id'],
        		"itemId"=>'tbmenu_'.$v['menu_id'],
	        	"text"=>str_replace(array('-','|'),array('',''),$v['menu_name']),
	        	"url"=> ($v['url']?BAOGG_BASE_URL. $v['url']:''), //$v['is_page']
	        	"pid"=>'menu_'.$v['menu_pid'],
        		"icon"=>BAOGG_UPLOAD_URL."image/menu/".$v['img_icon'],
        	);
        }
        
        Zend_Loader::loadClass('Model');
        $model=new Model();
        
        $arr_model_perm=array();
        if(!$User->isAdmin()){	        		
	        $arr_model_perm=array_keys((array)$Permission->checkResource('model'));	        									
	    }
 
        //load mall menu
        if($mall_menu_id){
	        $rs_model=$model->getList(array("main_table"=>"content","state"=>1), array("sort"=>"ordering","dir"=>"asc"));
	        foreach((array)$rs_model as $v){
	        	if((!$User->isAdmin()) && (!in_array($v['model_id'],$arr_model_perm))){
	        		continue;
	        	}
	        	$rs_menu[]=array(
	        		"id"=>'menu_content_'.$v['model_id'],
		        	"text"=>$v['model_name'],
		        	"url"=> BAOGG_BASE_URL. 'mall/content/list/model_id/'.$v['model_id'],
		        	"pid"=>'menu_'.$mall_menu_id
	        	);
	        }
        }
       
        //load edu menu
        if($edu_menu_id){
	        $rs_model=$model->getList(array("main_table"=>"user","state"=>1), array("sort"=>"ordering","dir"=>"asc"));
	        foreach((array)$rs_model as $v){	        	
	        	//check   model 's permission  	
	        	if((!$User->isAdmin()) && (!in_array($v['model_id'],$arr_model_perm))){
	        		continue;
	        	}
	        	
	        	//echo '<pre>';var_dump($arr_group_id);exit;
	        	$rs_menu[]=array(
	        		"id"=>'menu_content_'.$v['model_id'],
		        	"text"=>$v['model_name'],
		        	"url"=> BAOGG_BASE_URL. 'mall/userinfo/list/model_id/'.$v['model_id'],
		        	"pid"=>'menu_'.$edu_menu_id,
	        		'icon'=>BAOGG_UPLOAD_URL."image/menu/".$v['ext_table'].'.png',
	        	);
	        }
        }
        
        //echo '<pre>';print_r($rs_menu);echo Baogg_Db::debugDb(Zend_Registry::get('db'));exit;
		$objTree=new Baogg_Db_Tree();
		$tr=$objTree->Rs2Tree($rs_menu);
		$jsMenu=$objTree->genMenu($tr,key($tr));
		//echo '<pre>';print_r($tr);print_r($jsMenu[0]);exit;
		unset($rs_menu);		
		$this->view->menu=Zend_Json::encode($jsMenu ,false, array('enableJsonExprFinder' => true));
		$this->view->exit=BAOGG_LANGUAGE::get("exit");
		
		
		
		$this->view->init_group=(int)$init_group;
		//var_dump($this->view->init_group);
		//echo '<pre>';print_r($_SESSION);exit;
	}

	/**
	 * The default action - show the home page with accordion template.
	 */
	public function homeAction() {
		global $LANG;

		$Permission=new Permission();
		$User=new User();	
		$Menu=new Menu();
		$Role=new Role(); 
		$ResourceOp=new ResourceOp();		
		$QaReportCategory=new QaReportCategory();
		$QaReport=new QaReport();
		$arr_resource_op=array();
		
		//find user's group ids  
		$UserGroup=new UserGroup();
		$UserGroupRole=new UserGroupRole();	
		if($User->isAdmin()){
			$rs_group=$UserGroup->getList(array("state"=>1));			
		}else{					
			$rs_group=$UserGroupRole->getList(array("user_id"=>$_SESSION['admin']['user']['user_id']));
		}
		$arr_group=array();
		foreach((array)$rs_group as $v){
			$arr_group[]=$v['user_group_id'];
		}
		if(!$arr_group){
			throw new Exception(BAOGG_LANGUAGE::get('deny_access'));
			exit;
		}

		//generate user group's  Tree Combo
		$combo_group=array();
		if(array_filter($arr_group)){
			$combo_group=$UserGroup->getTreeByIds($arr_group);
		}
		if(in_array(0,$arr_group)){
			$combo_group=array_merge(array(array(0,BAOGG_LANGUAGE::get('personal'))),$combo_group);
		}
		//$this->view->store_group=Zend_Json::encode($combo_group);
		
		
		//initialize user group
		//var_dump($this->getRequest()->getParam('group'));exit;
		$init_group=$this->getRequest()->getParam('group');
		if($init_group===NULL || !in_array($init_group,$arr_group)){
			$init_group=NULL;			
			$level_count=0;					
			foreach($combo_group as $v){
				if($init_group===NULL){
					$init_group=$v[0];		
				}		
				//string "--" stands for level 			
				if(substr_count($v[1],'-')>$level_count){					
					$init_group=$v[0];
					$level_count=substr_count($v[1],'-');
					//var_dump(substr_count($v[1],'-'));var_dump($v[0]);var_dump($this->view->init_group);	
				}
			}	
		}
		$init_group=(int)$init_group;
		
		$_SESSION['admin']['user_group_role']['user_group_id']=$UserGroup->getWithChildIds( array($init_group));
		//var_dump($_SESSION['admin']['user_group_role']['user_group_id']);			exit;
		//get user's role ids
		$_SESSION['admin']['user_group_role']['role_id']=array();
		//echo '<pre>';print_r($_SESSION);exit;
		if(!$User->isAdmin()){		
			//if personal,then all parent group is himself
			if($init_group==0){
				$user_group_pids=0;
			}else{	
				$rs_user_group_pids=$UserGroup->getOne((int)$init_group);
				$user_group_pids=	array_filter(explode(",",$rs_user_group_pids[0]['user_group_pids']));
			}		
			$rs_role=$UserGroupRole->getList(array("user_id"=>$_SESSION['admin']['user']['user_id'],'user_group_id'=>$user_group_pids));
			foreach((array)$rs_role as $v){
				$_SESSION['admin']['user_group_role']['role_id'][]=$v['role_id'];
			}
		}else{
			$_SESSION['admin']['user_group_role']['role_id'][]=1; //admin,random value
		}
		
		//echo Baogg_Db::debugDb(Zend_Registry::get('db'));
		
		
		$_SESSION['admin']['permission']=array();
		//generate bakcend menu
		 $top_where=array("key_name"=>"backend","state"=>1);
		 $rs_top_menu=$Menu->getList($top_where);		 
		 $where=array("menu_pids like '%,{$rs_top_menu[0]['menu_id']},%'","state"=>1,"is_show"=>1);		 
		 if(!$User->isAdmin()){
		 	$arr_menu_id=$Permission->checkResource('menu');
		 	
		 	if(!$arr_menu_id){
		 		$where=array("1=0");
		 	}else{
				$_SESSION['admin']['permission']['menu_id']=array();			
				foreach((array)$arr_menu_id as $v){
						if(!isset($v['resource_op_id'])){
							continue;
						}
						if(!isset($arr_resource_op[$v['resource_op_id']])){
							$arr_resource_op[$v['resource_op_id']]=array();
							$rs_resource_op=$ResourceOp->getList(array('resource_op_id='.(int)$v['resource_op_id'].' or resource_op_pid='.(int)$v['resource_op_id']));
							if(!$rs_resource_op || !is_array($rs_resource_op))continue;
							foreach($rs_resource_op as $v_op){
								$arr_resource_op[$v['resource_op_id']][]=$v_op['resource_op_code'];
							}
							
						}
						foreach($arr_resource_op[$v['resource_op_id']] as $op_code){						
							$_SESSION['admin']['permission']['menu_id'][$v['resource_row_id']][]=$op_code;
						}
				}

		 		$rs_menu_pid=$Menu->getList(array("menu_id"=>array_keys($_SESSION['admin']['permission']['menu_id'])));		 		
		 		//all menu ids
		 		$str_menu_pids="";
		 		foreach((array)$rs_menu_pid as $v){
		 			$str_menu_pids.=$v['menu_pids'];
		 		}		 		
		 		$where['menu_id']=array_filter(array_unique(explode(",",$str_menu_pids)));		 		
		 	}
		 }        
       
        $rs=$Menu->getList($where,array("sort"=>"ordering","dir"=>"asc"),null,array("menu_id","menu_name","menu_pid","*"));
        
        
       //echo '<pre>';print_r($_SESSION['admin']['permission']['menu_id']);print_r($rs);exit;
        
        $objTree=new Baogg_Db_Tree();
        $tr=$objTree->rs2GridTree($rs);
        $rs=$objTree->tr2GridStore($tr);
        //echo '<pre>';print_r($rs);exit;

        $rs_menu=array();
        $pid=0;
        //$mall_menu_id=0;
        //$edu_menu_id=0;
        $rs_qa_survey_menu=null;
        foreach((array)$rs as $k=>$v){
        	if($v['key_name']=='backend'){
        		$root_menu=$v;
        		continue;
        	}
        	if($v['key_name']=='qa_survey'){
        		$rs_qa_survey_menu=$v;
        		continue;
        	}else if($v['key_name']=='edu'){
        		//$edu_menu_id=$v['menu_id'];
        	}
        	$pid=$pid?$pid:'menu_'.$v['menu_pid'];
        	$rs_menu[]=array(
        		"id"=>'menu_'.$v['menu_id'],
        		"itemId"=>'tbmenu_'.$v['menu_id'],
	        	"text"=>str_replace(array('-','|'),array('',''),$v['menu_name']),
	        	"url"=> $v['url']?BAOGG_BASE_URL. (substr($v['url'],-1)=="/"?$v['url']:$v['url'].'/')."menu_id/{$v['menu_id']}/":'',
	        	"pid"=>'menu_'.$v['menu_pid'],
        		"icon"=>BAOGG_UPLOAD_URL."image/menu/".$v['img_icon'],
        	);
        }
        
      
        
        $arr_qa_surveys_perm=array();
        $where_surveys=array('state'=>1);
        $_SESSION['admin']['permission']['qa_surveys_id']=array();
        if(!$User->isAdmin()){
			$rs_qa_surveys_perm=$Permission->checkResource('qa_surveys');			
			//store permission as session		
			
			foreach((array)$rs_qa_surveys_perm as $v){
				$_SESSION['admin']['permission']['qa_surveys_id'][$v['resource_row_id']]=array(0);
			}
	        $arr_qa_surveys_perm=array_merge(array(-1),array_keys($_SESSION['admin']['permission']['qa_surveys_id']));	

	        $where_surveys['qa_surveys_id']=$arr_qa_surveys_perm;
	        //echo '<pre>';print_r($where_surveys);exit;
	    }
 		
	    $QaSurveys=new QaSurveys();
	    $init_surveys=$this->getRequest()->getParam('surveys');	 
	      
	    $store_surveys=$QaSurveys->getCombo($where_surveys, array("sort"=>"ordering","dir"=>"asc"),array(),array('qa_surveys_id','qa_surveys_name'));
	    if(!$User->isAdmin() && !$store_surveys){
	    	throw new Exception(BAOGG_LANGUAGE::get('deny_access'));
	    	exit;
	    }
	    
	    if(!$init_surveys || !$User->isAdmin() && !in_array($init_surveys,$arr_qa_surveys_perm)){
	    	$init_surveys=$store_surveys[0][0];
	    }
	    $this->view->init_surveys=$init_surveys;
	    $this->view->store_surveys=Zend_Json::encode($store_surveys);
	    
	    //load qa survey menu
	    //echo __FILE__.'<pre>';print_r($rs_qa_survey_menu);exit;
	    $arr_report_category_id=array();
	    $tr_qa_report=array();
	    if($rs_qa_survey_menu){
	    	$rs_qa_report=$QaReport->getList(array("state"=>1,'qa_surveys_id'=>$init_surveys), array("sort"=>"ordering","dir"=>"asc"));
	    	//echo __FILE__.'<pre>';print_r($arr_qa_report_perm);print_r(array_keys($_SESSION['admin']['permission']['qa_report_id']));print_r($rs_menu);exit;
	    	foreach((array)$rs_qa_report as $k=>$v){
	    		//check   model 's permission
	    		/* if(!in_array($v['qa_surveys_id'],array($init_surveys)){
	    			unset($rs_qa_report[$k]);
	    			continue;
	    		} */
	    		$tr_qa_report[$v['qa_report_category_id']][$v['qa_report_id']]=$v;
	    		
	    
	    		//echo '<pre>';var_dump($arr_group_id);exit;
	    		/* $rs_menu[]=array(
	            	        		"id"=>'menu_qa_survey_'.$v['qa_report_id'],
	    							"itemId"=>'tbmenu_qa_survey_'.$v['qa_report_id'],
	            		        	"text"=>$v['qa_report_name'],
	            		        	"url"=> BAOGG_BASE_URL. $rs_qa_survey_menu['url']."menu_id/{$rs_qa_survey_menu['menu_id']}".'/qa_report_id/'.$v['qa_report_id'],
	            		        	"pid"=>'menu_'.$rs_qa_survey_menu['menu_pid'],
	            	        		'icon'=>BAOGG_UPLOAD_URL."image/menu/".$rs_qa_survey_menu['img_icon'],
	    		); */
	    	}
	    	
	    	$arr_qa_report_category_id=array_filter(array_keys($tr_qa_report));
	    	if($arr_qa_report_category_id){
		    	$rs_qa_report_category=$QaReportCategory->getList(array('qa_report_category_id'=>$arr_qa_report_category_id,'state'=>1),array('ordering'=>'asc'));
		    	foreach($rs_qa_report_category as $v){
		    		$rs_menu[]=array(
		    				"id"=>'menu_qa_report_category_'.$v['qa_report_category_id'],
		    				"itemId"=>'tbmenu_qa_report_category_'.$v['qa_report_category_id'],
		    				"text"=>$v['qa_report_category_name'],
		    				"url"=>'',// BAOGG_BASE_URL. $rs_qa_survey_menu['url']."menu_id/{$rs_qa_survey_menu['menu_id']}".'/qa_report_id/'.$v['qa_report_id'],
		    				"pid"=>'menu_'.$root_menu['menu_id'],
		    				'icon'=>'',//BAOGG_UPLOAD_URL."image/menu/".$rs_qa_survey_menu['img_icon'],
		    				);
		    		foreach(@$tr_qa_report[$v['qa_report_category_id']] as $v_report){
		    			$rs_menu[]=array(
		    					"id"=>'menu_qa_report_'.$v_report['qa_report_id'],
		    					"itemId"=>'tbmenu_qa_survey_'.$v_report['qa_report_id'],
		    					"text"=>$v_report['qa_report_name'],
		    					"url"=> BAOGG_BASE_URL. $rs_qa_survey_menu['url']."menu_id/{$rs_qa_survey_menu['menu_id']}".'/qa_report_id/'.$v_report['qa_report_id'],
		    					"pid"=>'menu_qa_report_category_'.$v['qa_report_category_id'],
		    					'icon'=>BAOGG_UPLOAD_URL."image/menu/".$rs_qa_survey_menu['img_icon'],
		    					);
		    		}
		    	}
		    }
	    }
	    //menu icon cls
	    $arr_menu_cls=array();
        foreach((array)$rs_menu as $v){
        	$arr_menu_cls['baogg-'.$v['itemId']]=$v['icon'];
        }
        $rs_menu_cls=$Menu->getList(array('state'=>1,'is_show'=>0,"img_icon!=''"));//not show menu,
        foreach((array)$rs_menu_cls as $v){
        	$arr_menu_cls['baogg-'.'tbmenu_'.$v['menu_id']]=BAOGG_UPLOAD_URL."image/menu/".$v['img_icon'];
        }
        
        $this->view->menu_clses=$arr_menu_cls;
        
        //echo __FILE__.'<pre>';print_r($rs_menu);exit;
		$objTree=new Baogg_Db_Tree();
		$tr=$objTree->Rs2Tree($rs_menu);
		$jsAccordion=$this->genAccordionPanel($tr);
		
		//$jsMenu=$objTree->genMenu($tr,key($tr));
		//echo '<pre>';print_r(array_keys($tr));print_r($jsMenu[0]);exit;
		unset($rs_menu);		
		//$this->view->menu=Zend_Json::encode($jsMenu ,false, array('enableJsonExprFinder' => true));

		$this->view->accordion=$jsAccordion;
		$this->view->exit=BAOGG_LANGUAGE::get("exit");
		//echo '<pre>';print_r($LANG);exit;
		$this->view->LANG=$LANG;
		$this->view->JSLANG=Zend_Json::encode($LANG);
		$this->view->init_group=(int)$init_group;
		
		$arr_parent_key=array_keys($tr);
		unset($arr_parent_key[0]);
		$this->view->parent_menu_ids=$arr_parent_key;
		//var_dump($this->view->init_group);
		//echo '<pre>';print_r($_SESSION);exit;
	}
	
	
/**
	 * The default action - show the home page with accordion template.
	 */
	public function qaAction() {
		global $LANG;

		$User=new User();
		if($User->isAdmin()){			
			throw new Exception(BAOGG_LANGUAGE::get('deny_access'));
			exit;
		}

		//generate user survey   Combo		
		$Survey=new Qa_Survey();
		$SurveyLanguage=new Qa_SurveyLanguage();
		$rs_survey=$Survey->getList(array(),array('active desc','datecreated desc'));
		$combo_survey=array();$arr_survey=array();
		foreach((array)$rs_survey as $v){
		    $rs_lang=$SurveyLanguage->getList(array($SurveyLanguage->myPrimary=>$v['sid'],'surveyls_language'=>'zh-Hans'));
		    $arr_survey[$v['sid']]=$v;
		    $combo_survey[]=array($v['sid'],$rs_lang[0]['surveyls_title']);
		}
		$this->view->store_survey=Zend_Json::encode($combo_survey);
		
		
		//initialize user survey
		//var_dump($this->getRequest()->getParam('survey'));exit;
		$init_survey=$this->getRequest()->getParam('sid');
		if($init_survey===NULL || !in_array($init_survey,$arr_survey)){
			$init_survey=$combo_survey[0][0];
		}
		$init_survey=(int)$init_survey;
		//var_dump($init_survey);			exit;
		$_SESSION['admin']['user_survey_role']['user_survey_id']=$Usersurvey->getWithChildIds( array($init_survey));
		
		//get user's role ids
		$_SESSION['admin']['user_survey_role']['role_id']=array();
		//echo '<pre>';print_r($_SESSION);exit;
		if(!$User->isAdmin()){		
			//if personal,then all parent survey is himself
			if($init_survey==0){
				$user_survey_pids=0;
			}else{	
				$rs_user_survey_pids=$Usersurvey->getOne((int)$init_survey);
				$user_survey_pids=	array_filter(explode(",",$rs_user_survey_pids[0]['user_survey_pids']));
			}		
			$rs_role=$UsersurveyRole->getList(array("user_id"=>$_SESSION['admin']['user']['user_id'],'user_survey_id'=>$user_survey_pids));
			foreach((array)$rs_role as $v){
				$_SESSION['admin']['user_survey_role']['role_id'][]=$v['role_id'];
			}
		}else{
			$_SESSION['admin']['user_survey_role']['role_id'][]=1; //admin,random value
		}
		
		//echo Baogg_Db::debugDb(Zend_Registry::get('db'));
		
		
		$_SESSION['admin']['permission']=array();
		//generate bakcend menu
		 $top_where=array("key_name"=>"backend","state"=>1);
		 $rs_top_menu=$Menu->getList($top_where);		 
		 $where=array("menu_pids like '%,{$rs_top_menu[0]['menu_id']},%'","state"=>1,"is_show"=>1);		 
		 if(!$User->isAdmin()){
		 	$arr_menu_id=$Permission->checkResource('menu');
			
		 	if(!$arr_menu_id){
		 		$where=array("1=0");
		 	}else{
				//store permission as session
				foreach((array)$arr_menu_id as $v){
						$_SESSION['admin']['permission']['menu_id'][$v['resource_row_id']]=explode(",",$v['resource_op_code']);
				}

		 		$rs_menu_pid=$Menu->getList(array("menu_id"=>array_keys($arr_menu_id)));		 		
		 		//all menu ids
		 		$str_menu_pids="";
		 		foreach((array)$rs_menu_pid as $v){
		 			$str_menu_pids.=$v['menu_pids'];
		 		}		 		
		 		$where['menu_id']=array_filter(array_unique(explode(",",$str_menu_pids)));		 		
		 	}
		 }        
       
        $rs=$Menu->getList($where,array("sort"=>"ordering","dir"=>"asc"),null,array("menu_id","menu_name","menu_pid","*"));
       // echo '<pre>';print_r($where);print_r($rs);
        
        $objTree=new Baogg_Db_Tree();
        $tr=$objTree->rs2GridTree($rs);
        $rs=$objTree->tr2GridStore($tr);
        //echo '<pre>';print_r($rs);exit;

        $rs_menu=array();
        $pid=0;
        //$mall_menu_id=0;
        //$edu_menu_id=0;
        foreach((array)$rs as $k=>$v){
        	if($v['key_name']=='backend'){
        		continue;
        	}
        	if($v['key_name']=='mall'){
        		//$mall_menu_id=$v['menu_id'];
        	}else if($v['key_name']=='edu'){
        		//$edu_menu_id=$v['menu_id'];
        	}
        	$pid=$pid?$pid:'menu_'.$v['menu_pid'];
        	$rs_menu[]=array(
        		"id"=>'menu_'.$v['menu_id'],
        		"itemId"=>'tbmenu_'.$v['menu_id'],
	        	"text"=>str_replace(array('-','|'),array('',''),$v['menu_name']),
	        	"url"=> $v['url']?BAOGG_BASE_URL. (substr($v['url'],-1)=="/"?$v['url']:$v['url'].'/')."menu_id/{$v['menu_id']}/":'',
	        	"pid"=>'menu_'.$v['menu_pid'],
        		"icon"=>BAOGG_UPLOAD_URL."image/menu/".$v['img_icon'],
        	);
        }
        
        Zend_Loader::loadClass('Model');
        $model=new Model();
        
        $arr_model_perm=array();
        if(!$User->isAdmin()){
			$rs_model_perm=$Permission->checkResource('model');
			//store permission as session
			foreach((array)$rs_model_perm as $v){
					$_SESSION['admin']['permission']['model_id'][$v['resource_row_id']]=explode(",",$v['resource_op_code']);
			}
	        $arr_model_perm=array_keys((array)$rs_model_perm);	        									
	    }
 
        //load mall menu
       /* if($mall_menu_id){
	        $rs_model=$model->getList(array("main_table"=>"content","state"=>1), array("sort"=>"ordering","dir"=>"asc"));
	        foreach((array)$rs_model as $v){
	        	if((!$User->isAdmin()) && (!in_array($v['model_id'],$arr_model_perm))){
	        		continue;
	        	}
	        	$rs_menu[]=array(
	        		"id"=>'menu_content_'.$v['model_id'],
		        	"text"=>$v['model_name'],
		        	"url"=> BAOGG_BASE_URL. 'mall/contentinfo/list/model_id/'.$v['model_id'].'/',
		        	"pid"=>'menu_'.$mall_menu_id
	        	);
	        }
        }
       
        //load edu menu
        if($edu_menu_id){
	        $rs_model=$model->getList(array("main_table"=>"user","state"=>1), array("sort"=>"ordering","dir"=>"asc"));
	        foreach((array)$rs_model as $v){	        	
	        	//check   model 's permission  	
	        	if((!$User->isAdmin()) && (!in_array($v['model_id'],$arr_model_perm))){
	        		continue;
	        	}
	        	
	        	//echo '<pre>';var_dump($arr_survey_id);exit;
	        	$rs_menu[]=array(
	        		"id"=>'menu_content_'.$v['model_id'],
		        	"text"=>$v['model_name'],
		        	"url"=> BAOGG_BASE_URL. 'mall/userinfo/list/model_id/'.$v['model_id'].'/',
		        	"pid"=>'menu_'.$edu_menu_id,
	        		'icon'=>BAOGG_UPLOAD_URL."image/menu/".$v['ext_table'].'.png',
	        	);
	        }
        }*/
        
       // echo '<pre>';print_r($rs_menu);echo Baogg_Db::debugDb(Zend_Registry::get('db'));exit;
		$objTree=new Baogg_Db_Tree();
		$tr=$objTree->Rs2Tree($rs_menu);
		$jsAccordion=$this->genAccordionPanel($tr);
		
		//$jsMenu=$objTree->genMenu($tr,key($tr));
		//echo '<pre>';print_r($tr);print_r($jsMenu[0]);exit;
		unset($rs_menu);		
		//$this->view->menu=Zend_Json::encode($jsMenu ,false, array('enableJsonExprFinder' => true));

		$this->view->accordion=$jsAccordion;
		$this->view->exit=BAOGG_LANGUAGE::get("exit");
		$this->view->LANG=$LANG;
		$this->view->init_survey=(int)$init_survey;
		//var_dump($this->view->init_survey);
		//echo '<pre>';print_r($_SESSION);exit;
	}
	public function headerAction() {
		
	}
	public function navigatorAction() {
	
	}
	public function rightsAction() {
	
	}
	public function bodyAction() {
	
	}
	public function aihuiAction(){

	}
	private function genAccordionPanel(& $tr=array()){		
		if(!$tr || !is_array($tr)){
			return "";
		}
		
		$pid=key($tr);
		$ret=array();
		foreach((array)$tr[$pid] as $v){
			//echo '<pre>';print_r(@$tr[$v['id']]);exit;
			$ret[]="{
			frame:true,
			xtype:'panel',
			title: '{$v['text']}',
			id: 'panel_{$v['id']}',
			collapsible:true,
			titleCollapse: true,
			//flex:1,
			width:'100%',	
			autoShow:true,
			height:".(48+count((array)@$tr[$v['id']])*19).",
			/*listeners: {
					afterrender :function(self,opt){
							 //console.info('panel after redner');						
                            self.doComponentLayout();//  Ext.getCmp('panel_{$v['id']}')
                            self.expand();
                            self.collapse();
                            self.expand();                            
                    }
				},*/
			
			items:{
				xtype:'dataview',
				layout: {
					type:'vbox',
					align:'stretch',
					pack :'start'
				},
				/*listeners: {
					afterrender :function(self,opt){						
                            Ext.getCmp('panel_{$v['id']}') && Ext.getCmp('panel_{$v['id']}').doComponentLayout();// self
                    }
				},*/
				autoShow:true,				
				padding:5,
				//layout:'fit',
				toFrontOnShow :true,
				autoRender:true,
				autoShow:true,
				height:'100%',
				tpl: new Ext.XTemplate(
						'<div><ul style=\"display:block;height:100%;\"><tpl for=\".\">',
							 '<li><img src=\"{icon}\" class=\"\"/>&nbsp;',
							'<a id=\"{itemId}\" href=\"{url}\">{text}</a></li>',
						'</tpl></ul></div>'
					),
				store:{
						idProperty: 'id',
						xtype:'jsonstore',
						 model: 'Menu',
						 /*listeners: {
							load :function(self,records,  successful, operation,  eOpts ){						
		                            Ext.getCmp('panel_{$v['id']}') && Ext.getCmp('panel_{$v['id']}').doComponentLayout();// self
		                    }
						},*/
						data:".Zend_Json::encode(array_values((array)@$tr[$v['id']]))."
						
					},
				itemSelector: 'div.user_group_id-wrap'
			}
				
		}";

		}
		return "{
			id:'action-panel',
			xtype:'panel',
			region:'west',
			split:true,
			collapsible: true,
			collapseMode: 'mini',
			header: false,
			width:200,
			minWidth: 150,
			border: false,			
			baseCls:'x-plain',
			layout: {
				type:'vbox',
				align:'stretch',
				//flex:'stretch',
				pack :'start'
			},
			/*listeners: {
					afterrender :function(self,opt){
						console.info('after render');
						Ext.getCmp('panel_menu_3').doComponentLayout();		
					}
			},*/
			autoShow:true,
			style:'overflow-y:auto',			
			items: [".implode(",",$ret)."]
		}";
	}
}
